// Variables: 
char note = 0;            // The MIDI note value to be played
int time = 0;
int BPM = 100;
int greenLedPin = 13;
int redLedPin = 11;
int beatPin = 7;
int blueLedPin = 10;
char drums = 0x9F;
char lead = 0x90;
int recording;
byte incomingByte;


//  plays a MIDI note.  Doesn't check to see that
//  cmd is greater than 127, or that data values are  less than 127:
void noteOn(char cmd, char data1, char data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}


void setup() {
  //  Set MIDI baud rate:
  Serial.begin(31250);
  pinMode (beatPin,INPUT);
  digitalWrite(beatPin,HIGH);

  pinMode (greenLedPin,OUTPUT);
  pinMode (blueLedPin,OUTPUT);
  pinMode (redLedPin,OUTPUT);

}

void record() 
{
   for (int i = 0; i < Serial.available() ; i = i) {
	incomingByte = Serial.read();

	Serial.print(incomingByte,BYTE);
   }

}

void loop() {

if (recoding == 0){

    for (int i = 0; i < Serial.available() ; i = i) {
	incomingByte = Serial.read();

	Serial.print(incomingByte,BYTE);
    }
}
else
{
record()
}
 
    // Beat Pin.. or record pin?
    if (digitalRead(beatPin) == 0 ) {
	if (recording = 0) {
	    startRecord();
	    recording = 1;
	}
    }


	// LED toys.. feedback on where in the 8 steps
    if (time >= 0 && time >= 20){
	digitalWrite(greenLedPin,HIGH);
	digitalWrite(blueLedPin,LOW);
	digitalWrite(redLedPin,LOW);
    }


    if (time > 20 && time > 40){
	digitalWrite(redLedPin,HIGH);
	digitalWrite(greenLedPin,LOW);
	digitalWrite(blueLedPin,LOW);
    }

    if ( 40 < time && time >= 60){
	digitalWrite(blueLedPin,HIGH);
	digitalWrite(greenLedPin,LOW);
	digitalWrite(redLedPin,LOW);
    }

    if ( 60 < time && time >= 80){
	digitalWrite(blueLedPin,HIGH);
	digitalWrite(greenLedPin,LOW);
	digitalWrite(redLedPin,HIGH);
    }


    }

    else {
    digitalWrite(greenLedPin,LOW);
    digitalWrite(blueLedPin,LOW);
    digitalWrite(redLedPin,LOW); }

	// Time Counter.
    delay(20);
    time++;
    if (time > 80){
	time = 0;
    }
}


